package com.shushan.home.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.common.ui.OnRecyclerviewItemClickListener;
import com.shushan.lockscreen.bean.AppInfo;
import com.shushan.shushanbao.R;

import java.util.List;

/**
 * @ClassName: WhiteAppsHAdapter
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-08
 */
public class WhiteAppsHAdapter extends RecyclerView.Adapter<WhiteAppsHAdapter.ViewHolder> {

    private Context context;
    private List<AppInfo> appInfoList;
    private OnRecyclerviewItemClickListener recyclerviewItemClickListener;

    public WhiteAppsHAdapter(Context context) {
        this.context = context;
    }

    public WhiteAppsHAdapter(Context context, List<AppInfo> appInfoList) {
        this.context = context;
        this.appInfoList = appInfoList;
    }

    @Override
    public WhiteAppsHAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.home_white_app_list_item, null, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(WhiteAppsHAdapter.ViewHolder holder,final int position) {
        AppInfo appInfo = appInfoList.get(position);

        Drawable logo = appInfo.getIcon();
        holder.logoImg.setImageDrawable(logo);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(recyclerviewItemClickListener != null){
                    recyclerviewItemClickListener.onItemClickListener(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if(appInfoList == null){
            return 0;
        }else {
            return appInfoList.size();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView logoImg;


        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            logoImg = itemView.findViewById(R.id.home_white_list_item_img);
        }
    }

    public List<AppInfo> getAppInfoList() {
        return appInfoList;
    }

    public void setAppInfoList(List<AppInfo> appInfoList) {
        this.appInfoList = appInfoList;
    }

    public OnRecyclerviewItemClickListener getRecyclerviewItemClickListener() {
        return recyclerviewItemClickListener;
    }

    public void setRecyclerviewItemClickListener(OnRecyclerviewItemClickListener recyclerviewItemClickListener) {
        this.recyclerviewItemClickListener = recyclerviewItemClickListener;
    }
}
